from machine import Pin
from machine import WDT
import myuart

b_send_mqtt_data = False

#WTD
wdt = None

#io
cat_led = Pin(23, Pin.OUT, value=1)  

IO1 = Pin(17, Pin.IN)  #yellow
IO2 = Pin(18, Pin.IN)  #green
IO3 = Pin(19, Pin.IN)  #beep
IO4 = Pin(5, Pin.IN)   #red

#LED 1HZ
led_count = 0     #pwm计数
led_on_t  = 10    #亮时间
led_off_t = 80    #灭时间
led_t     = 100   #LED pwm周期

def led_loop():
    global led_count
    led_count = led_count+1
    if led_count>led_t:
       led_count =0
    if led_count < led_on_t:
       cat_led.value(0)
    else:
       cat_led.value(1)  
#LED
#time_on  亮时间
#time_off 灭时间
def set_led_status(time_on,time_off):
    global led_on_t,led_off_t,led_t
    led_on_t  = time_on     #亮时间
    led_off_t = time_off    #灭时间
    led_t   = led_on_t +led_off_t      #总时间


#IO
value_io1 = 0
value_io2 = 0
value_io3 = 0
value_io4 = 0

value_io1_last = 0
value_io2_last = 0
value_io3_last = 0
value_io4_last = 0

#检测IO状态
def check_io_status():
    global value_io1,value_io2,value_io3,value_io4
    global value_io1_last,value_io2_last,value_io3_last,value_io4_last
    
    value_io1 = IO1.value()  #YELLOW
    value_io2 = IO2.value()  #GREEN
    value_io3 = IO3.value()  #BEEP
    value_io4 = IO4.value()  #RED

    #YELLOW
    if(value_io1 != value_io1_last):
        myuart.modbus_data[6] = 3
   
    #GREEN
    if(value_io2 != value_io2_last):
        myuart.modbus_data[6] = 2

    #BEEP
    if(value_io3 != value_io3_last):
        myuart.modbus_data[6] = 4
   
    #RED
    if(value_io4 != value_io4_last):
        myuart.modbus_data[6] = 1

    value_io1_last = value_io1
    value_io2_last = value_io2
    value_io3_last = value_io3
    value_io4_last = value_io4
